/*
package com.mbc.config;

import com.mbc.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class JwtTokenUserInterceptor implements HandlerInterceptor {

    @Autowired
    private JwtProperties jwtTokenProvider;

    public static ThreadLocal<Long> longThreadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
        //判断当前拦截到的是Controller的方法还是其他资源
        if (!(handler instanceof HandlerMethod)){
            //其他资源直接放行
            return true;
        }
        //1.获取请求头中的token
        String token = request.getHeader(jwtTokenProvider.getUserTokenName());

        //2.令牌校验
        try {
            log.info("jwt校验:{}",token);
            Claims claims = JwtUtil.parseJWT(jwtTokenProvider.getUserSecretKey(), token);
            log.info("id:{}",claims.getId());
            //通过claims里面的键值对的类型的键获取值 因为claims里面存的是一个HashMap
            longThreadLocal.set(Long.valueOf(claims.get("id").toString()));
            //通过 放行
            return true;
        } catch (Exception e) {
            //4、不通过，响应401状态码
            response.setStatus(401);
            return false;
        }
    }
}

*/
